package com.nuoniu.sibanyun.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.nuoniu.sibanyun.common.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.3</version>
</dependency>
*/
public class SendSmsUtils {
	public static String accessKeyId = "LTAISpDI6XzlIM1h";
	public static String accessSecret = "RoS5ZnwO7SeYyjs67dXHrI0YLysRyg";
	public static String SignName = "四班云";
	public static String TemplateCode = "SMS_174165296"; //验证码模版
	public static String TemplateEmpCode = "SMS_220636341"; //通知模版

	public static void main(String[] args) {
		sendEmpSms("", "1111", "13320372373");
	}

	public static Map<String, Object> sentSms(String phone) {
		Map<String, Object> map = new HashMap<>();
		DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
		IAcsClient client = new DefaultAcsClient(profile);
		// 生成验证码
		String code = StringUtils.getRandom() + StringUtils.getRandomNum(5);
		map.put("code", code);
		map.put("result", "");
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", "default");
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", SignName);
		request.putQueryParameter("TemplateCode", TemplateCode);
		request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");// 随机6位验证码
		try {
			//map.put("result", "success");
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
			if (response.getData().contains("OK")) {
				map.put("result", "success");
			} else {
				map.put("result", "fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 批量邀请注册发送短信
	 * @author 小啊刚
	 * @date 2021-07-24
	 */
	public static Map<String, Object> sendEmpSms(String signNames, String params, String phones) {
		Map<String, Object> map = new HashMap<>();
		DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendBatchSms");
		request.putQueryParameter("RegionId", "default");
		request.putQueryParameter("PhoneNumberJson", phones);
		request.putQueryParameter("SignNameJson", signNames);
		request.putQueryParameter("TemplateCode", TemplateEmpCode);
		request.putQueryParameter("TemplateParamJson", params);

		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response);
			if (response.getData().contains("OK")) {
				map.put("result", "success");
			} else {
				map.put("result", "fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
}